package usercollect

import (
	"context"
	// "fmt"
	"strings"

	"mall/common/modelx"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/model"
	"mall/service/forum/rpc/internal/svc"
	"mall/service/forum/rpc/internal/utils/errorhandler"
	"mall/service/forum/rpc/types/forum"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserCollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserCollectListLogic {
	return &GetUserCollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserCollectListLogic) GetUserCollectList(in *forum.BaseListReq) (*forum.UserCollectListResp, error) {
	// logrus.Info(fmt.Sprintf("GetUserCollectList BaseListReq: %+v", in))
	// Limit:1 Preload:"forumUserCollect:Id" Filter:"UserId,equalTo:10021;PlatformId,equalTo:1;ObjectId,equalTo:5;ObjectType,equalTo:281;Status,equalTo:1" VisitorId:10021 PlatformId:1

	if in.Filter == nil || len(*in.Filter) == 0 {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	preloadMap, _ := modelx.ParsePreloadAndEagerLoad(in.Preload, in.EagerLoad)
	// logrus.Info(fmt.Sprintf("GetUserCollectList preloadMap: %+v", preloadMap))
	// map[forumUserCollect:[Id]]
	// logrus.Info(fmt.Sprintf("GetUserCollectList eagerLoadMap: %+v", eagerLoadMap))
	// map[]

	collectEntityName := model.RpcEntityNames.ForumUserCollect
	if _, ok := model.RpcEntityPreloadMap[collectEntityName]; !ok {
		return &forum.UserCollectListResp{}, nil
	}

	collectRepositoryName := model.RepositoryNames.ForumUserCollect
	columnMap, ok := model.RepositoryPreloadMap[collectRepositoryName]
	if !ok {
		return &forum.UserCollectListResp{}, nil
	}

	var selectCols []string
	if preloadFields, ok := preloadMap[collectEntityName]; ok {
		selectCols = modelx.ParseSelectColumns(preloadFields, columnMap)
	}

	filterMap := modelx.ParseFilter(in.Filter, columnMap)
	// logrus.Info(fmt.Sprintf("GetUserCollectList filterMap: %+v", filterMap))
	// map[object_id:map[equalTo:[5]] object_type:map[equalTo:[281]] platform_id:map[equalTo:[1]] status:map[equalTo:[1]] user_id:map[equalTo:[10021]]]
	if filterMap == nil {
		return nil, resultx.StatusError(resultx.REUQEST_PARAM_ERROR, "")
	}

	orderBy := modelx.ParseSorter(in.Sorter, columnMap)
	// logrus.Info(fmt.Sprintf("GetUserCollectList orderBy: %s", orderBy))
	//
	if len(orderBy) == 0 {
		orderBy = model.ForumUserCollectColumns.CollectID + " DESC"
	}

	res, count, err := l.svcCtx.UserCollect.GetList(l.ctx, model.M{
		"offset":  in.Offset,
		"limit":   in.Limit,
		"orderBy": orderBy,
		"select":  strings.Join(selectCols, ","),
	}, 2, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	hasNextPage, hasPreviousPage := modelx.ParseCount(count)
	resp := &forum.UserCollectListResp{
		HasNextPage:     &hasNextPage,
		HasPreviousPage: &hasPreviousPage,
	}

	if len(res) > 0 {
		for _, v := range res {
			resp.List = append(resp.List, &forum.UserCollectInfo{
				Id:         pointy.GetPointer(v.CollectID),
				UserId:     pointy.GetPointer(v.UserID),
				PlatformId: pointy.GetPointer(uint32(v.PlatformID)),
				ObjectId:   pointy.GetPointer(v.ObjectID),
				ObjectType: pointy.GetPointer(uint32(v.ObjectType)),
				Status:     pointy.GetPointer(uint32(v.Status)),
				CreatedAt:  pointy.GetPointer(modelx.FormatTime(v.CreatedAt, "")),
				UpdatedAt:  pointy.GetPointer(modelx.FormatTime(v.UpdatedAt, "")),
			})
		}
	}

	return resp, nil
}
